package com.minglv.mini.tools;

import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;

@Data
public class MiniResult {


    private Meta meta;

    private Object data;


    /**
     * 操作成功
     * @param data
     * @return
     */
    public static MiniResult success(Object data){
        MiniResult r = new MiniResult();
        Meta meta = new Meta();
        meta.setCode(200);
        meta.setMsg("获取成功");
        r.setMeta(meta);
        if (ObjectUtils.isEmpty(data)) {
            r.setData(new ArrayList<>());
        }else{
            r.setData(data);
        }
        return r;
    }

    MiniResult(){

    }

    public MiniResult(Integer code,String msg,Object data){
        Meta meta = new Meta();
        meta.setCode(code);
        meta.setMsg(msg);
        this.setMeta(meta);
        this.setData(data);
        if (ObjectUtils.isEmpty(data)) {
            setData(new ArrayList<>());
        }else{
            this.setData(data);
        }
    }

    public static MiniResult fail(){
        MiniResult r = new MiniResult();
        Meta meta = new Meta();
        meta.setCode(201);
        meta.setMsg("操作异常");
        r.setMeta(meta);
        return r;
    }

    public static MiniResult fail(Integer code, String msg) {
        MiniResult r = new MiniResult();
        Meta meta = new Meta();
        meta.setCode(code);
        meta.setMsg("msg");
        r.setMeta(meta);
        return r;
    }

    public static class Meta{

        private Integer code;
        private String msg;

        public Integer getCode(){
            return code;
        }

        public void setCode(Integer code){
            this.code = code;
        }

        public String getMsg(){
            return this.msg;
        }

        public void setMsg(String msg){
            this.msg = msg;
        }
    }
}
